package com.tyc.mb.controller;

import com.tyc.mb.model.User;
import com.tyc.mb.model.response.CommonResult;
import com.tyc.mb.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户管理
 *
 * @author tyc
 * @version 1.0
 * @date 2021-10-25 18:28:11
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/{sid}/{id}")
    public CommonResult getId(@PathVariable Integer sid, @PathVariable Integer id){
        User user = userService.getUserById(sid, id);
        return CommonResult.success(user);
    }

    /**
     * 测试布隆过滤器
     * @param userName
     * @return
     */
    @GetMapping("/{userName}")
    public CommonResult getUserByName(@PathVariable String userName){
        return CommonResult.success(userService.getUserByName(userName));
    }

    @PutMapping("/{sid}")
    public CommonResult login(@Valid @RequestBody User user, @PathVariable Integer sid){
        return CommonResult.success(userService.login(user, sid));
    }

    @PostMapping("{sid}")
    public CommonResult register(@Valid @RequestBody User user, @PathVariable Integer sid){
        return CommonResult.success(userService.register(user, sid));
    }
}
